package com.wlb365.admin.modules.schema.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.google.common.collect.Lists;
import com.wlb365.admin.modules.schema.entity.Node;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * jsonschema工具类.
 *
 * @author zwh
 * @version 1.0
 */
public class JsonSchemaUtil {

    /**
     * jsonSchema转树结构.
     *
     * @param data
     * @param schema
     */
    public static void jsonSchemaToTree(List<LinkedHashMap<String, Object>> data, String schema) {
        JSONObject jsonObject = JSON.parseObject(schema, Feature.OrderedField);
        JSONObject properties = jsonObject.getJSONObject("properties");
        for (String key : properties.keySet()) {
            putJson(data, "", key, properties.getJSONObject(key), jsonObject.getJSONArray("required"));
        }
    }

    /**
     * 将jsonschema转换为tree.
     *
     * @param data     转换后的list
     * @param pid      父id
     * @param key      当前节点key
     * @param item     元素
     * @param required 必填项
     */
    private static void putJson(List<LinkedHashMap<String, Object>> data, String pid, String key, JSONObject item, JSONArray required) {
        LinkedHashMap<String, Object> json = new LinkedHashMap<>();
        String type = item.getString("field");
        json.put("id", key);
        if (StringUtils.isNotEmpty(pid)) {
            json.put("pid", pid);
        }
        json.put("field", key);
        json.put("type", type);
        json.put("description", item.containsKey("description") ? item.getString("description") : "");
        json.put("required", "N");//是否必填
        if (required != null && required.contains(key)) {
            json.put("required", "Y");
        }
        if ("string".equals(type)) {
            if (item.containsKey("minLength")) {
                //字符串最小长数
                json.put("min", item.getString("minLength"));
            }
            if (item.containsKey("maxLength")) {
                //字符串最大长数
                json.put("max", item.getString("maxLength"));
            }
        } else if ("integer".equals(type) || "number".equals(type)) {
            if (item.containsKey("minimum")) {
                //数值最小值
                json.put("min", item.getString("minimum"));
            }
            if (item.containsKey("maximum")) {
                //数值最大值
                json.put("max", item.getString("maximum"));
            }
        }
        //枚举
        if (item.containsKey("enum")) {
            json.put("enum", item.getJSONArray("enum").stream().collect(Collectors.toList()));
        }

        data.add(json);

        if ("object".equals(type)) {
            if (item.containsKey("stype")) {//扩展type类型
                String stype = item.getString("stype");
                if ("file".equals(stype)) {
                    //文件类型
                    json.put("type", stype);
                    json.put("url", item.getString("url"));
                    json.put("hash", item.getString("hash"));
                }
            } else {
                List<LinkedHashMap<String, Object>> treeList = new ArrayList<>();
                JSONObject sub = item.getJSONObject("properties");
                for (String subKey : sub.keySet()) {
                    putJson(treeList, key, key + "." + subKey, sub.getJSONObject(subKey), item.getJSONArray("required"));
                }
                json.put("treeList", treeList);
            }

        }
    }




    /**
     * jsonSchema转树结构.
     *
     * @param data
     */
    public static void treetoSchema(List<Node> data,JSONObject jsonObject) {
        jsonObject.put("$schema", "http://json-schema.org/draft-04/schema#");
        jsonObject.put("type","object");

        for (Node node : data) {
            putJson(node, jsonObject);
        }
    }

    /**
     * 将jsonschema转换为tree.
     */
    private static void putJson(Node node, JSONObject json ) {
        JSONObject fieldObject = new JSONObject();
        JSONObject valueObject = new JSONObject();
        valueObject.put("type",node.getType());
        valueObject.put("description",node.getDescription());
        fieldObject.put(node.getField(),valueObject);
        if(! node.getTreeList().isEmpty()){
            List<Node> treeList = node.getTreeList();

        }


    }




    public static void main(String[] args) {
        String str = "[{\"field\":\"id\",\"description\":\"第一层id\",\"required\":null,\"type\":\"integer\",\"min\":1,\"max\":\"10\",\"enum\":null,\"id\":\"2nfe2vby2y2\",\"LAY_TABLE_INDEX\":0,\"treeList\":[{\"pid\":\"2nfe2vby2y2\",\"id\":\"2ly7xtkuhyy\",\"treeList\":[],\"field\":\"id2\",\"description\":\"第二次id\",\"type\":\"integer\",\"min\":\"2\",\"max\":\"10\",\"enum\":\"1,2,3\"}],\"LAY_CHECKED\":false},{\"field\":\"type\",\"description\":\"type\",\"required\":null,\"type\":\"string\",\"min\":\"1\",\"max\":\"1\",\"enum\":\"1,2\",\"id\":\"dstxg1gj61\",\"LAY_TABLE_INDEX\":1,\"treeList\":[{\"pid\":\"dstxg1gj61\",\"id\":\"295eq2dwpwg\",\"field\":\"type2\",\"description\":\"第二层type\",\"type\":\"string\",\"min\":\"1\",\"max\":\"1\",\"enum\":\"3,4\"}],\"LAY_CHECKED\":null}]";


    }

}
